package top.chmin.project.cloud.builder;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * chmin
 * 2023/12/15 15:21
 * cloud-builder
 */
@Component
public class PageConfigBuilder implements Builder {
    @Override
    public void build(BuilderConfig config) {
        String content = """
                import {BasePage} from "../%sts/type";
                import {replace} from "../%sts/util";
                                
                export let extraConfig = (page: BasePage) => {
                    replace(page, {
                        
                    })
                }""".formatted(
                Stream.of(StrUtil.toUnderlineCase(config.getEntity().getClassName()).split("_"))
                        .map(s -> "../")
                        .collect(Collectors.joining()),
                Stream.of(StrUtil.toUnderlineCase(config.getEntity().getClassName()).split("_"))
                        .map(s -> "../")
                        .collect(Collectors.joining())
        );

        saveUi(config,
                "\\view\\%s%sPageConfig.ts".formatted(
                        Stream.of(StrUtil.toUnderlineCase(config.getEntity().getClassName()).split("_"))
                                .map(s -> s + "\\")
                                .collect(Collectors.joining()),
                        StringUtils.uncapitalize(config.getEntity().getClassName())
                ),
                content,
                false
        );
    }
}
